from app import db
from datetime import datetime

class Employee(db.Model):
    """员工表"""
    __tablename__ = 'employee'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    employee_no = db.Column(db.String(50), unique=True, nullable=False, comment='员工编号')
    name = db.Column(db.String(50), nullable=False, comment='姓名')
    gender = db.Column(db.Enum('MALE', 'FEMALE'), comment='性别')
    birth_date = db.Column(db.Date, comment='出生日期')
    hire_date = db.Column(db.Date, nullable=False, comment='入职日期')
    department = db.Column(db.String(50), comment='部门')
    position = db.Column(db.String(50), comment='职位')
    phone = db.Column(db.String(20), comment='联系电话')
    email = db.Column(db.String(100), comment='邮箱')
    address = db.Column(db.String(200), comment='地址')
    id_number = db.Column(db.String(18), unique=True, comment='身份证号')
    bank_name = db.Column(db.String(100), comment='开户银行')
    bank_account = db.Column(db.String(50), comment='银行账户')
    status = db.Column(db.Enum('ACTIVE', 'INACTIVE', 'LEFT'), default='ACTIVE', comment='状态')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    def to_dict(self):
        return {
            'id': self.id,
            'employee_no': self.employee_no,
            'name': self.name,
            'gender': self.gender,
            'birth_date': self.birth_date.isoformat() if self.birth_date else None,
            'hire_date': self.hire_date.isoformat() if self.hire_date else None,
            'department': self.department,
            'position': self.position,
            'phone': self.phone,
            'email': self.email,
            'address': self.address,
            'id_number': self.id_number,
            'bank_name': self.bank_name,
            'bank_account': self.bank_account,
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted
        }

class Payroll(db.Model):
    """工资表"""
    __tablename__ = 'payroll'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    employee_id = db.Column(db.BigInteger, db.ForeignKey('employee.id'), nullable=False, comment='员工ID')
    pay_period = db.Column(db.String(7), nullable=False, comment='薪资期间 (YYYY-MM)')
    basic_salary = db.Column(db.Numeric(15, 2), comment='基本工资')
    allowance = db.Column(db.Numeric(15, 2), default=0, comment='津贴')
    bonus = db.Column(db.Numeric(15, 2), default=0, comment='奖金')
    overtime_pay = db.Column(db.Numeric(15, 2), default=0, comment='加班费')
    deduction = db.Column(db.Numeric(15, 2), default=0, comment='扣除')
    tax = db.Column(db.Numeric(15, 2), default=0, comment='个人所得税')
    total_pay = db.Column(db.Numeric(15, 2), comment='应发工资')
    actual_pay = db.Column(db.Numeric(15, 2), comment='实发工资')
    status = db.Column(db.Enum('DRAFT', 'APPROVED', 'PAID'), default='DRAFT', comment='状态')
    created_by = db.Column(db.BigInteger, nullable=False, comment='创建人')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    # 关联员工
    employee = db.relationship('Employee', backref='payrolls')
    
    def to_dict(self):
        return {
            'id': self.id,
            'employee_id': self.employee_id,
            'pay_period': self.pay_period,
            'basic_salary': float(self.basic_salary) if self.basic_salary else 0.0,
            'allowance': float(self.allowance) if self.allowance else 0.0,
            'bonus': float(self.bonus) if self.bonus else 0.0,
            'overtime_pay': float(self.overtime_pay) if self.overtime_pay else 0.0,
            'deduction': float(self.deduction) if self.deduction else 0.0,
            'tax': float(self.tax) if self.tax else 0.0,
            'total_pay': float(self.total_pay) if self.total_pay else 0.0,
            'actual_pay': float(self.actual_pay) if self.actual_pay else 0.0,
            'status': self.status,
            'created_by': self.created_by,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted,
            'employee': self.employee.to_dict() if self.employee else None
        }

class Attendance(db.Model):
    """考勤表"""
    __tablename__ = 'attendance'
    
    id = db.Column(db.BigInteger, primary_key=True, autoincrement=True)
    employee_id = db.Column(db.BigInteger, db.ForeignKey('employee.id'), nullable=False, comment='员工ID')
    work_date = db.Column(db.Date, nullable=False, comment='工作日期')
    check_in_time = db.Column(db.Time, comment='上班时间')
    check_out_time = db.Column(db.Time, comment='下班时间')
    work_hours = db.Column(db.Numeric(5, 2), comment='工作小时数')
    overtime_hours = db.Column(db.Numeric(5, 2), default=0, comment='加班小时数')
    status = db.Column(db.Enum('PRESENT', 'ABSENT', 'LEAVE'), default='PRESENT', comment='状态')
    remark = db.Column(db.String(200), comment='备注')
    created_at = db.Column(db.DateTime, default=datetime.now)
    updated_at = db.Column(db.DateTime, default=datetime.now, onupdate=datetime.now)
    is_deleted = db.Column(db.Boolean, default=False)
    
    # 关联员工
    employee = db.relationship('Employee', backref='attendances')
    
    def to_dict(self):
        return {
            'id': self.id,
            'employee_id': self.employee_id,
            'work_date': self.work_date.isoformat() if self.work_date else None,
            'check_in_time': self.check_in_time.isoformat() if self.check_in_time else None,
            'check_out_time': self.check_out_time.isoformat() if self.check_out_time else None,
            'work_hours': float(self.work_hours) if self.work_hours else 0.0,
            'overtime_hours': float(self.overtime_hours) if self.overtime_hours else 0.0,
            'status': self.status,
            'remark': self.remark,
            'created_at': self.created_at.isoformat() if self.created_at else None,
            'updated_at': self.updated_at.isoformat() if self.updated_at else None,
            'is_deleted': self.is_deleted,
            'employee': self.employee.to_dict() if self.employee else None
        }